import Vue from "vue";
import App from "./App.vue";
import router from "@/router";
import store from "./store";
// 全局引入ElementUI
import ElementUI from "element-ui";
// 按需引入
// import { Button, Row, Container, Aside, Header, Main } from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "./api/mock";
import Cookie from "js-cookie";

Vue.config.productionTip = false;
// 全局引入
Vue.use(ElementUI);
// 按需引入
// Vue.use(Button);
// Vue.use(Row);
// Vue.use(Container);
// Vue.use(Aside);
// Vue.use(Header);
// Vue.use(Main);

// 全局前置导航守卫
router.beforeEach((to, from, next) => {
  // 判断token存不存在
  const token = Cookie.get("token");
  // token不存在说明未登录，要跳转到登录页
  if (!token && to.name !== "login") {
    next({ name: "login" });
  } else if (token && to.name === "login") {
    //用户已登录就不能再访问登录页面
    next({ name: "home" });
  } else {
    next();
  }
});

// 将路由注入到vue实例
new Vue({
  router,
  store,
  render: (h) => h(App),
  created() {
    store.commit("addMenu", router);
  },
}).$mount("#app");
